//
//  Jdd_RFSF_TableView.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-12-16.
//
//

#import "Jdd_RFSF_TableView.h"

@interface Jdd_RFSF_TableView ()

@end

@implementation Jdd_RFSF_TableView


- (void)viewDidLoad {
	[super viewDidLoad];
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    self.tableView.tableFooterView = v;
    [self.tableView setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
	groupArray = [[NSMutableArray alloc] init];
	for (int i = 0; i < [_tableData count]; ++i) {
		JddFollowNumber *follownum = [[JddFollowNumber alloc]init];
		follownum.open = 1;
		[groupArray addObject:follownum];
	}
    //去除cell的分割线
    if (groupArray.count == 0) {
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [_tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSDictionary *sectionCell = [_tableData objectAtIndex:section];
	NSString *MCount = [sectionCell objectForKey:@"MCount"];
	if ([groupArray count] > 0) {
		JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
		if (groupDic.open == 0) {
			return 0;
		}
		else {
			return [MCount intValue];
		}
	}
	else {
		return [MCount intValue];
	}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 80.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	return 34.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	JddPickNumSportItemCellHeader *cell = [JddPickNumSportItemCellHeader getInstanceView];
	NSDictionary *sectionCell = [_tableData objectAtIndex:section];
	NSNumber *MCount = [sectionCell objectForKey:@"MCount"];
	NSString *Name = [sectionCell objectForKey:@"issue"];
	cell.day.text = Name;
	cell.Date.text = [JddCommonFunction convertWeekByNumber:[sectionCell objectForKey:@"Wk"]];
	cell.Mnum.text = [NSString stringWithFormat:@"%d场比赛可投", [MCount intValue]];
	cell.statePosition = [NSNumber numberWithInt:section];
	cell.delegate = self;
	if ([groupArray count] > 0) {
		JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
		if (groupDic.open == 0) {
			cell.HeadButton.image = [UIImage imageNamed:@"hemai_hall_down.png"];
		}
		else {
			cell.HeadButton.image = [UIImage imageNamed:@"hemai_up.png"];
		}
	}
	return cell;
}

- (void)sportTeamButtonHeadClicked:(NSNumber *)position {
	JddFollowNumber *groupDic = [groupArray objectAtIndex:[position intValue]];
	//设置groupDic中的展开状态
	if (groupDic.open == 0) {
		groupDic.open = 1;
	}
	else {
		groupDic.open = 0;
	}
	[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[position intValue]]
	              withRowAnimation:UITableViewRowAnimationFade];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	JddPickNumBasketBallItemCell *cell = [JddPickNumBasketBallItemCell getInstanceView];
	NSDictionary *sectionCell = [_tableData objectAtIndex:indexPath.section];
	NSArray *Match = [sectionCell objectForKey:@"Matches"];
	NSDictionary *MatchCell = [Match objectAtIndex:indexPath.row];
    
	NSString *ETime = [MatchCell objectForKey:@"ETime"];
	NSString *firstTeamName = [MatchCell objectForKey:@"VTeam"];
	NSString *secondTeamName = [MatchCell objectForKey:@"HTeam"];
    
	NSString *MID = [MatchCell objectForKey:@"MID"];
	NSString *NMm = [MatchCell objectForKey:@"NMm"];
	NSString *SpRFSF = [MatchCell objectForKey:@"SpRFSF"];
	//类型为胜负时取字段SpSF
	if (self.lottId == LOTTERY_PLAY_METHOD_BASKETBALL_SF) {
		SpRFSF = [MatchCell objectForKey:@"SpSF"];
        cell.rfLabel.hidden = YES;
        cell.firstTeamButton.frame = CGRectMake(102, 35, 100, 30);
        cell.secondTeamButton.frame = CGRectMake(202, 35, 100, 30);
	}
	//类型为大小分时取字段SpSF
	if (self.lottId == LOTTERY_PLAY_METHOD_BASKETBALL_DXF) {
		SpRFSF = [MatchCell objectForKey:@"SpDXF"];
	}
	NSArray *Pl = [SpRFSF componentsSeparatedByString:@"|"];
	NSString *Rf = [Pl objectAtIndex:0];

	cell.nolabel.text = [NSString stringWithFormat:@"%@", [MID substringFromIndex:6]];
	cell.endTimeLabel.text = [NSString stringWithFormat:@"%@%@", [ETime substringWithRange:NSMakeRange(11, 5)], @"截止"];
	cell.endtime = ETime;
	cell.MID = MID;
	cell.firstTeamLabel.text = firstTeamName;
    cell.secondTeamLabel.text = secondTeamName;

	//类型为让分，显示 让分
	if (![Rf isEqualToString:@"0"] && self.lottId == LOTTERY_PLAY_METHOD_BASKETBALL_RFSF) {
        cell.rfLabel.text = Rf;
	}

    if (![Rf isEqualToString:@"0"] && self.lottId == LOTTERY_PLAY_METHOD_BASKETBALL_DXF) {
        cell.rfLabel.text = [NSString stringWithFormat:@"%.2f",[Rf floatValue]];
	}

	cell.matchNameLabel.text = NMm;
    
	cell.firstTeamNumber = [[JddSportNumber alloc] init];
	cell.secondTeamNumber = [[JddSportNumber alloc] init];
	cell.firstTeamNumber.code = MID;
	cell.firstTeamNumber.Hteam = secondTeamName;
	cell.firstTeamNumber.Vteam = firstTeamName;
	cell.firstTeamNumber.endTime = ETime;
    
	cell.secondTeamNumber.code = MID;
	cell.secondTeamNumber.Hteam = secondTeamName;
	cell.secondTeamNumber.Vteam = firstTeamName;
	cell.secondTeamNumber.endTime = ETime;
    
	if (Pl.count >= 2) {
		//胜负只有2位
		int fIndex = 1;
		int sIndex = 2;
		NSString *fName = @"主负";
		NSString *sName = @"主胜";
		NSString *fTag = @"2";
		NSString *sTag = @"1";
		if (self.lottId == LOTTERY_PLAY_METHOD_BASKETBALL_SF) {
			fIndex = 0;
			sIndex = 1;
		}
		if (self.lottId == LOTTERY_PLAY_METHOD_BASKETBALL_DXF) {
			fName = @"大分";
			sName = @"小分";
			fTag = @"1";
			sTag = @"2";
		}
		cell.firstTeamNumber.teamArray = [[NSMutableArray alloc] initWithObjects:fTag, nil];
		cell.secondTeamNumber.teamArray = [[NSMutableArray alloc] initWithObjects:sTag, nil];
//		cell.fistTeamBtnName.text = [NSString stringWithFormat:@"%@%1.2f", fName, [[Pl objectAtIndex:fIndex] floatValue]];
//		cell.secondTeamBtnName.text = [NSString stringWithFormat:@"%@%1.2f", sName, [[Pl objectAtIndex:sIndex] floatValue]];
        [cell.firstTeamButton setTitle:[NSString stringWithFormat:@"%@%1.2f", fName, [[Pl objectAtIndex:fIndex] floatValue]] forState:UIControlStateNormal];
        [cell.secondTeamButton setTitle:[NSString stringWithFormat:@"%@%1.2f", sName, [[Pl objectAtIndex:sIndex] floatValue]] forState:UIControlStateNormal];
	}
	else {
		cell.fistTeamBtnName.text = @"主负-";
		cell.secondTeamBtnName.text = @"主胜-";
	}
	cell.delegate = self;
	for (JddSportNumber *num in self.sportlotterylist.sportList) {
		if ([num.code isEqualToString:MID]) {
			for (NSNumber *num2 in num.teamArray) {
				switch ([num2 intValue]) {
					case 1:
						if (self.lottId == LOTTERY_PLAY_METHOD_BASKETBALL_DXF) {
							[cell.firstTeamButton setSelected:YES];
						}
						else {
							[cell.secondTeamButton setSelected:YES];
						}
						break;
                        
					case 2:
						if (self.lottId == LOTTERY_PLAY_METHOD_BASKETBALL_DXF) {
							[cell.secondTeamButton setSelected:YES];
						}
						else {
							[cell.firstTeamButton setSelected:YES];
						}                        break;
                        
					default:
						break;
				}
			}
		}
	}
	return cell;
}

- (void)basketBallTeamButtonClicked:(id)sender number:(JddSportNumber *)number1 {
	UIButton *button = sender;
	JddSportNumber *number = [[JddSportNumber alloc] init];
	number.code = number1.code;
	number.teamArray = [NSMutableArray arrayWithArray:number1.teamArray];
	number.endTime = number1.endTime;
	number.Hteam = number1.Hteam;
	number.Vteam = number1.Vteam;
    
	if (![self.sportlotterylist BallHasNumber:number]) {
		[button setSelected:NO];
	}
	else {
		if ([self.sportlotterylist.sportList count] > 15) {
			[self.sportlotterylist BallHasNumber:number];
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"最多选择15场比赛" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
			[alert show];
			[button setSelected:NO];
		}
		else {
			[button setSelected:YES];
		}
	}
	[self.delegate performSelector:@selector(selectMatch)];
}

@end
